//
//  RWFrameModel.m
//  Chat
//
//  Created by 任伟 on 16/1/3.
//  Copyright © 2016年 任伟. All rights reserved.
//

#import "RWFrameModel.h"
#import "RWBlogModel.h"

@implementation RWFrameModel

/**
 *  重写模型的set方法,计算各个控件frame的值以及cell的高度
 
 *
 *  @param blog <#blog description#>
 */
- (void)setBlog:(RWBlogModel *)blog {

    _blog = blog;
    
    // 1.设置头像的frame
    CGFloat padding = 10;
    CGFloat headViewX = padding;
    CGFloat headViewY = padding;
    CGFloat headViewW = 60;
    CGFloat headViewH = 60;
    _headF = CGRectMake(headViewX, headViewY, headViewW, headViewH);
    
    // 2.设置名称的frame
    CGFloat nameViewX = CGRectGetMaxX(_headF) + padding;
    //    boundingRectWithSize:表示给定区域范围,文字用来显示到这个范围中
    //    options:表示计算的方式
    //   attributes:表示文字的属性
    CGSize nameMaxSize = CGSizeMake(MAXFLOAT, MAXFLOAT);
    
    
    //    CGSize nameSize = [ self.blog.name boundingRectWithSize:nameMaxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size;
    CGSize nameSize = [self sizeWithText:self.blog.name andFont:[UIFont systemFontOfSize:17] maxSize:nameMaxSize];
    
    CGFloat nameViewY =  (headViewH - nameSize.height )*0.5 + padding;
    _nameF = CGRectMake(nameViewX, nameViewY, nameSize.width, nameSize.height);
   
    // 3.设置vipframe
    if (self.blog.vip) {
        CGFloat vipViewX = CGRectGetMaxX(_nameF) +padding;
        CGFloat vipViewY = nameViewY;
        CGFloat vipViewW = 16;
        CGFloat vipViewH = 16;
        _vipF = CGRectMake(vipViewX, vipViewY, vipViewW, vipViewH);
        
    }
    
    // 4.设置正文frame
    CGFloat textViewX = headViewX;
    CGFloat textViewY = CGRectGetMaxY(_headF) + padding;
    CGSize textMaxSize = CGSizeMake(355, MAXFLOAT);
    CGSize textSize = [self sizeWithText:self.blog.text andFont:[UIFont systemFontOfSize:17] maxSize:textMaxSize];
    _textViewF = CGRectMake(textViewX, textViewY, textSize.width, textSize.height);
    
    // 5.设置配图的frame
    if (self.blog.picture) {
        CGFloat pictureViewX = textViewX;
        CGFloat pictureViewY = CGRectGetMaxY(_textViewF) + padding;
        CGFloat pictureViewW = 100;
        CGFloat pictureViewH = 60;
        _pictureViewF= CGRectMake(pictureViewX, pictureViewY, pictureViewW, pictureViewH);
        _cellHeight = CGRectGetMaxY(_pictureViewF) + padding;
    }else{
        
        _cellHeight = CGRectGetMaxY(_textViewF) + padding;
    }
}

- (CGSize)sizeWithText:(NSString *)text andFont:(UIFont *)nameFont maxSize:(CGSize)boundSize{
    NSDictionary *dict = @{NSFontAttributeName:nameFont};
    
    return  [ text boundingRectWithSize:boundSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size;
    
}

@end
